<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Api_Model_NotificationMapper {
	protected $_db_table;

	public function __construct() {
		$this->_db_table = new Api_Model_DbTable_Notification();
	}

	public function getAllNotification($user_id) {
		$db_user = new Api_Model_DbTable_User();
		$db_tour = new Api_Model_DbTable_Tour();

		$select = $this->_db_table->select()->from('notifications', array('*'))->where('receiver_id = ?', $user_id);
		
		$result= $this->_db_table->fetchAll($select);

		if(count($result) == 0) {
			throw new Exception(json_encode(array('meta' => 'Not found')),404);
		} else {
			foreach ($result as $row) {
                            if($row->type == 'friend'){
                                $select_friend = $db_user->select()->from('users', array('*'))->where('user_id = ?', $row->sender_id);
				$result_friend = $this->_db_table->fetchAll($select_friend);
				$row_friend = $result_friend->current();
				$friend = array('user_id' => $row_friend->user_id,
                                'username' => $row_friend->username,
                                'fullname' => $row_friend->fullname,
                                'profile_picture' => $row_friend->profile_picture
				);
                                $rowArray = $row->toArray();
                                $rowArray['sender_id'] = $friend;
                                $json[] = array('notification' => $rowArray);
                            }
                            
                            elseif($row->type == 'message'){
                                $select_friend = $db_user->select()->from('users', array('*'))->where('user_id = ?', $row->sender_id);
				$result_friend = $this->_db_table->fetchAll($select_friend);
				$row_friend = $result_friend->current();
				$message = array('user_id' => $row_friend->user_id,
                                'username' => $row_friend->username,
                                'fullname' => $row_friend->fullname,
                                'profile_picture' => $row_friend->profile_picture
				);
                                $rowArray = $row->toArray();
                                $rowArray['sender_id'] = $message;
                                $json[] = array('notification' => $rowArray);
                            }
                            
                            elseif($row->type == 'tour'){
                                $select_tour = $db_tour->select()->from('tours', array('*'))->where('tour_id = ?', $row->concern_id);
				$result_tour = $this->_db_table->fetchAll($select_tour);
				$row_tour = $result_tour->current();
				$tour = array('tour_id' => $row_tour->tour_id,
                                'name' => $row_tour->name
				);
                                
                                $select_friend = $db_user->select()->from('users', array('*'))->where('user_id = ?', $row->sender_id);
				$result_friend = $this->_db_table->fetchAll($select_friend);
				$row_friend = $result_friend->current();
				$sender = array('user_id' => $row_friend->user_id,
                                'username' => $row_friend->username,
                                'fullname' => $row_friend->fullname,
                                'profile_picture' => $row_friend->profile_picture
				);
                                
                                
                                $rowArray = $row->toArray();
                                $rowArray['concern_id'] = $tour;
                                $rowArray['sender_id'] = $sender;
                                $json[] = array('notification' => $rowArray);
                            }

			}


			return $json;
		}
	}
        
        public function deleteNotification($notification_id){
                $where = array('notification_id = ?' => $notification_id);
                $this->_db_table->delete($where);
	}
        
        public function readNotification($notification_id){
		$where = array('notification_id = "'.$notification_id.'"');
		$this->_db_table->update(array('status' => '1'), $where);
	}

}
